<template>
  <div class="add-member-container">
    <el-card class="form-card">
      <template #header>
        <div class="card-header">
          <h2>添加党员</h2>
        </div>
      </template>
      
      <el-form :model="memberForm" :rules="rules" ref="memberFormRef" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="memberForm.name" placeholder="请输入党员姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-radio-group v-model="memberForm.gender">
                <el-radio label="male">男</el-radio>
                <el-radio label="female">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthDate">
              <el-date-picker
                v-model="memberForm.birthDate"
                type="date"
                placeholder="选择出生日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input-number v-model="memberForm.age" :min="18" :max="100" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="memberForm.phone" placeholder="请输入手机号码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电子邮箱" prop="email">
              <el-input v-model="memberForm.email" placeholder="请输入电子邮箱" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="党员类型" prop="type">
              <el-select v-model="memberForm.type" placeholder="请选择党员类型">
                <el-option label="正式党员" value="formal" />
                <el-option label="预备党员" value="probationary" />
                <el-option label="发展对象" value="development" />
                <el-option label="入党积极分子" value="activist" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学历" prop="education">
              <el-select v-model="memberForm.education" placeholder="请选择学历">
                <el-option label="博士" value="doctor" />
                <el-option label="硕士" value="master" />
                <el-option label="本科" value="bachelor" />
                <el-option label="大专" value="college" />
                <el-option label="高中及以下" value="highSchool" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属支部" prop="branch">
              <el-select v-model="memberForm.branch" placeholder="请选择所属支部">
                <el-option label="技术部党支部" value="tech" />
                <el-option label="行政部党支部" value="admin" />
                <el-option label="市场部党支部" value="market" />
                <el-option label="财务部党支部" value="finance" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入党日期" prop="joinDate">
              <el-date-picker
                v-model="memberForm.joinDate"
                type="date"
                placeholder="选择入党日期"
              />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="政治面貌" prop="politicalStatus">
          <el-radio-group v-model="memberForm.politicalStatus">
            <el-radio label="party">中共党员</el-radio>
            <el-radio label="probation">中共预备党员</el-radio>
            <el-radio label="applicant">入党申请人</el-radio>
            <el-radio label="league">共青团员</el-radio>
            <el-radio label="masses">群众</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="memberForm.address" placeholder="请输入家庭住址" />
        </el-form-item>
        
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="memberForm.remark"
            type="textarea"
            rows="3"
            placeholder="请输入备注信息"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm" :loading="loading">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const router = useRouter();
const memberFormRef = ref(null);
const loading = ref(false);

// 表单数据
const memberForm = reactive({
  name: '',
  gender: 'male',
  birthDate: '',
  age: 30,
  phone: '',
  email: '',
  type: '',
  education: '',
  branch: '',
  joinDate: '',
  politicalStatus: 'party',
  address: '',
  remark: ''
});

// 表单验证规则
const rules = {
  name: [
    { required: true, message: '请输入党员姓名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  birthDate: [
    { required: true, message: '请选择出生日期', trigger: 'change' }
  ],
  age: [
    { required: true, message: '请输入年龄', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入电子邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的电子邮箱格式', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择党员类型', trigger: 'change' }
  ],
  branch: [
    { required: true, message: '请选择所属支部', trigger: 'change' }
  ],
  joinDate: [
    { required: true, message: '请选择入党日期', trigger: 'change' }
  ],
  politicalStatus: [
    { required: true, message: '请选择政治面貌', trigger: 'change' }
  ]
};

// 提交表单
const submitForm = async () => {
  if (!memberFormRef.value) return;
  
  await memberFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        // 这里应调用API提交数据
        setTimeout(() => {
          ElMessage.success('添加党员成功');
          loading.value = false;
          router.push('/party-members/list');
        }, 1000);
      } catch (error) {
        console.error('提交失败:', error);
        ElMessage.error('添加党员失败，请稍后再试');
        loading.value = false;
      }
    }
  });
};

// 重置表单
const resetForm = () => {
  if (memberFormRef.value) {
    memberFormRef.value.resetFields();
  }
};

// 返回列表页
const goBack = () => {
  router.push('/party-members/list');
};
</script>

<style scoped>
.add-member-container {
  padding: 20px;
}

.form-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  font-size: 18px;
  color: #303133;
  margin: 0;
}

:deep(.el-form-item__label) {
  font-weight: 500;
}
</style> 